package com.ruoyi.agreement.service.impl;

import com.ruoyi.agreement.domain.Agreement;
import com.ruoyi.agreement.domain.Result;
import com.ruoyi.agreement.feign.UserFeign;
import com.ruoyi.agreement.mapper.AgreementMapper;
import com.ruoyi.agreement.service.AgreementService;
import com.ruoyi.user.domain.UserInformationInfo;
import com.ruoyi.user.domain.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class AgreementServiceImpl implements AgreementService {

    @Autowired
    private AgreementMapper agreementMapper;

    @Autowired
    private UserFeign userFeign;

    @Override
    public Result addAgreement(String code) throws Exception{
        Agreement agreement = new Agreement();

        UserVo userVo = userFeign.getCode(code);
        if(userVo.getNumbers()!=null){
            //获取身份证号
            String numbers = userVo.getNumbers();
            //截取年月日
            String nums = numbers.substring(6, 14);
            System.out.println(nums);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
//            simpleDateFormat.format(new Date());
            simpleDateFormat.parse(nums);
            //获取今天的日期
            String date = simpleDateFormat.format(new Date());
            //获取年龄
            int age = Integer.parseInt(date.substring(0, 4)) - Integer.parseInt(nums.substring(0, 4));
                if(age<18){
                    return new Result(500,"未满18周岁,授信不予通过",null);
                }
                else if(age>=18 && age<=35){
                    agreement.setNum(new BigDecimal(150000));
                    agreement.setUserInformationInfoCode(code);
                    agreement.setCreateTime(new Date());
                    agreement.setUpdateTime(new Date());
                    agreementMapper.addAgreement(agreement);
                    return new Result(200,"您的额度为150000",agreement);
                }
                else if(age>=36 && age <=55){
                    agreement.setNum(new BigDecimal(300000));
                    agreement.setUserInformationInfoCode(code);
                    agreement.setCreateTime(new Date());
                    agreement.setUpdateTime(new Date());
                    agreementMapper.addAgreement(agreement);
                    return new Result(200,"您的额度为300000",agreement);
                }

        }
        return new Result(400,"授信失败",null);
    }

    @Override
    public Agreement getAgreement(String code){
        return agreementMapper.getAgreement(code);
    }

    @Override
    public void upAgreement(Agreement agreement) {
        agreementMapper.upAgreement(agreement);
    }
}
